# win的初始化，初始图像绘制

# win的绘制和初始化创建
# 棋盘绘制
# 水印绘制
# 加载icon图片

import pygame as pg
import os

from config import *


def draw_chessboard(screen): # 绘制棋盘
    # 绘制网格线
    for i in range(BOARD_SIZE):
        # 横线
        pg.draw.line(screen, LINE_COLOR,
                     (START_X, START_Y + i * GRID_SIZE),
                     (START_X + BOARD_WIDTH, START_Y + i * GRID_SIZE),
                     2)
        # 竖线
        pg.draw.line(screen, LINE_COLOR,
                     (START_X + i * GRID_SIZE, START_Y),
                     (START_X + i * GRID_SIZE, START_Y + BOARD_HEIGHT),
                     2)

    # 绘制棋盘上的五个小黑点（天元和星）
    star_points = [(3, 3), (3, 11), (7, 7), (11, 3), (11, 11)]
    for point in star_points:
        x = START_X + point[0] * GRID_SIZE
        y = START_Y + point[1] * GRID_SIZE
        pg.draw.circle(screen, LINE_COLOR, (x, y), 5)

def icon_draw_factory(icon_png):
    # 使用字典存储所有需要保持状态的变量
    state = {
        'icon_alpha': 300,
        'icon_L': 400,
        'icon_R': 300,
        'save_water': 1,
        'icon_png': icon_png,  # 保存原始图片
        'small_icon': None     # 用于存储缩小后的图片
    }
    icon_png_size = icon_png.get_size()
    def icon_draw(win):
        # 声明需要修改的外部变量
        nonlocal state
        icon_alpha = state['icon_alpha']
        icon_L = state['icon_L']
        icon_R = state['icon_R']
        save_water = state['save_water']
        current_icon = state['icon_png']
        # 动画逻辑
        if icon_alpha > 255:
            # 初始阶段：无透明度效果
            current_icon.set_alpha(255)
            win.blit(current_icon, 
                    (icon_L - icon_png_size[0] // 2,
                     icon_R - icon_png_size[1] // 2))
            state['icon_alpha'] -= 2
            
        elif 0 <= icon_alpha <= 255:
            # 透明度变化阶段
            current_icon.set_alpha(icon_alpha)
            win.blit(current_icon, 
                    (icon_L - icon_png_size[0] // 2,
                     icon_R - icon_png_size[1] // 2))
            state['icon_alpha'] -= 2
            
        else:
            # 最终阶段：显示小图标
            if save_water == 1:
                # 只缩小一次
                state['small_icon'] = pg.transform.smoothscale_by(current_icon, 0.1)
                state['save_water'] = 0
                
            small_icon = state['small_icon']
            small_icon.set_alpha(225)
            win.blit(small_icon, (0, 0))
    
    return icon_draw



def png(): # 加载icon图片
    base_dir = os.path.dirname(__file__)
    icon_path = os.path.join(base_dir, r'game_file\icon.png')
    print(icon_path)
    icon_png = pg.image.load(icon_path)
    return icon_png

def window_set(icon_png): # 初始化窗口
    win = pg.display.set_mode((WIDTH, HEIGHT))
    pg.display.set_caption("zyx_game")
    pg.display.set_icon(icon_png)
    return win


